<?php
class Member extends CI_Controller {

    public function index() {
       $this->load->helper(array('form', 'url'));
       $this->load->library('log');
       if($this->session->userdata('netron_admin_user_id') != '' && $this->session->userdata('netron_admin_user_name') != '') {
        $this->template->load('admin_template', 'admin/home');
       } else {
           $this->template->set('title', 'Administrator Login');
         $this->template->load('admin_template', 'admin/login');
       }

    }

    public function member_area() {
        $this->load->helper(array('form', 'url'));
        $validate_customer_login = array(
            array( 'field' => 'user_name',
                   'label' => 'User Name',
                   'rules' => 'required|callback_login_validate'),
            array( 'field' => 'user_password',
                   'label' => 'Password',
                   'rules' => 'required')
        );

        $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
        $this->form_validation->set_rules($validate_customer_login);

        if ($this->form_validation->run() == FALSE) {
           
            $this->template->load('template', 'member/login');

	} else  {
           $this->template->load('template', 'member/home');
          $this->load->model('user_security');
        }
     
    }
    public function home() {
        $this->load->model('user_security');
    }
    public function login_validate() {
        $this->load->model('login');
        $user_name = $this->input->post('user_name');
        $user_password = $this->input->post('user_password');
        $user_login_status = $this->login->member_login($user_name, $user_password);
        if($user_login_status == TRUE) {
            return TRUE;
        }
        else if($user_login_status == FALSE) {
            $this->form_validation->set_message('login_validate', 'Invalid Username or Password');
            return FALSE;
        } else if($user_login_status == 'inactive') {
            $this->form_validation->set_message('login_validate', 'Your account is inactive');
            return FALSE;
        } 
    }
    public function logout() {
        $this->session->sess_destroy();
        $this->template->load('template', 'member/login');
    }
    public function service_details() {
        $this->load->model('user_security');
        $this->load->model(array('service','customer'));
        $customer_id = $this->session->userdata('netron_customer_id');
        $ethernet_services = $this->service->get_ethernet_services_by_customer($customer_id);
        $dsl_services = $this->service->get_dsl_services_by_customer($customer_id);
        $all_services = array();
        if(!empty ($ethernet_services)) {
            $ethernet_services_view = array();
            foreach ($ethernet_services->result() as $ethernet_services_values) {
                $ethernet_services_item = array();
                $ethernet_services_item['ethernet_number'] = $ethernet_services_values->ethernet_number;
                $ethernet_services_item['creation_date'] = $ethernet_services_values->creation_date;

                array_push($ethernet_services_view, $ethernet_services_item);
            }
            $all_services['ethernet_services'] = $ethernet_services_view;
        }
        if(!empty ($dsl_services)) {
            $dsl_services_view = array();
            foreach ($dsl_services->result() as $dsl_services_values) {
                $dsl_services_item = array();
                $dsl_services_item['dsl_number'] = $dsl_services_values->dsl_number;
                $dsl_services_item['creation_date'] = substr($dsl_services_values->creation_date,0,10);
                $address_details = $this->customer->get_address_details($dsl_services_values->address_id);
                $dsl_services_item['dsl_address'] = $address_details['street_number'].'  '.$address_details['street_name'];
                array_push($dsl_services_view, $dsl_services_item);
            }
            $all_services['dsl_services'] = $dsl_services_view;
        }
        //$this->load->view('member/list_services',$all_services);
        $this->template->load('template', 'member/list_services',$all_services);
        

      

        }
     public function order_status() {
         $this->load->model('customer');
         $this->load->model('user_security');
         $customer_id = $this->session->userdata('netron_customer_id');
         $all_order_status = array();
         $dsl_orders = $this->customer->get_dsl_order_status($customer_id);
         if($dsl_orders != FALSE) {
             $dsl_orders_view = array();
             foreach ($dsl_orders as $dsl_order) {
                $dsl_order_item = array();
                $dsl_order_item['dsl_number'] = $dsl_order->dsl_number;
                $dsl_order_item['dsl_order_status'] = $dsl_order->order_status;
                array_push($dsl_orders_view, $dsl_order_item);
             }
             $all_order_status['dsl_order'] = $dsl_orders_view;
         }
         $ethernet_orders = $this->customer->get_ethernet_order_status($customer_id);
         if($ethernet_orders != FALSE) {
             $ethernet_orders_view = array();
             foreach ($ethernet_orders as $ethernet_order) {
                $ethernet_order_item = array();
                $ethernet_order_item['ethernet_number'] = $ethernet_order->ethernet_number;
                $ethernet_order_item['ethernet_order_status'] = $ethernet_order->order_status;
                array_push($ethernet_orders_view, $ethernet_order_item);
             }
             $all_order_status['ethernet_order'] = $ethernet_orders_view;
         }
         $this->load->view('member/order_status',$all_order_status);
     }

     public function change_payment() {
         $this->load->model('customer');
         $this->load->model('user_security');
         $customer_id = $this->session->userdata('netron_customer_id');
         $current_payment_details['current_payment_details'] = $this->customer->get_current_payment_details($customer_id);
         $this->template->load('template','member/change_payment_details',$current_payment_details);
     }
    public function update_payment_details() {
        $this->load->helper(array('form', 'url'));
        $this->load->model('customer');
         $this->load->model('user_security');
         $customer_id = $this->session->userdata('netron_customer_id');
         $current_payment_details['current_payment_details'] = $this->customer->get_current_payment_details($customer_id);

        $validate_payment_option = array (
                                        array(
                                            'field' => 'payment_option',
                                            'label'   => 'Payment Option',
                                            'rules'   => 'required'
                                        )
                                       );

            if($this->input->post('payment_option') == 'credit') {
                $validate_payment_details =  array (
                    array(
                        'field' => 'card_type',
                        'label'   => 'Card Type',
                        'rules'   => 'required'
                    ),
                    array(
                        'field' => 'card_number',
                        'label'   => 'Card Number',
                        'rules'   => 'required'
                    ), array(
                        'field' => 'expiry_month',
                        'label'   => 'Expiry Month',
                        'rules'   => 'required|integer|exact_length[2]'
                    ), array(
                        'field' => 'expiry_year',
                        'label'   => 'Expiry Year',
                        'rules'   => 'required|integer|exact_length[4]'
                    ), array(
                        'field'   => 'card_holder_name',
                        'label'   => 'Card holder Name',
                        'rules'   => 'required'
                    ), array(
                        'field'   => 'cvv',
                        'label'   => 'CVV',
                        'rules'   => 'required|integer'
                    )
                );
            } else if($this->input->post('payment_option') == 'debit') {
                $validate_payment_details =  array (
                    array(
                        'field' => 'bank_name',
                        'label'   => 'Bank Name',
                        'rules'   => 'required'
                    ),array(
                        'field' => 'branch_name',
                        'label'   => 'Branch Name',
                        'rules'   => 'required'
                    ), array(
                        'field' => 'account_name',
                        'label'   => 'Account Name',
                        'rules'   => 'required'
                    ), array(
                        'field' => 'account_number',
                        'label'   => 'Account Number',
                        'rules'   => 'required|numeric'
                    ),array(
                        'field'   => 'bsb',
                        'label'   => 'BSB',
                        'rules'   => 'required|exact_length[6]'
                    )
                );
            }

            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            $this->form_validation->set_rules($validate_payment_option);
            if($this->input->post('payment_option') != '') {
                $this->form_validation->set_rules($validate_payment_details);
            }
            if ($this->form_validation->run() == FALSE) {
                $this->template->load('template','member/change_payment_details',$current_payment_details);
            } else {
                if(trim($this->input->post('payment_option')) == 'credit') {
                   $payment_details = array(
                       'payment_option'  => 'credit',
                       'card_type' => $this->input->post('card_type'),
                       'card_number' => $this->input->post('card_number'),
                       'card_holder_name' => $this->input->post('card_holder_name'),
                       'expiry_month' => $this->input->post('expiry_month'),
                       'expiry_year' => $this->input->post('expiry_year'),
                       'cvv' => $this->input->post('cvv')
                   );
                } else if(trim($_POST['payment_option']) == 'debit') {
                    $payment_details = array(
                       'payment_option'  => 'debit',
                       'bank_name' => $this->input->post('bank_name'),
                       'branch_name' => $this->input->post('branch_name'),
                       'account_name' => $this->input->post('account_name'),
                       'account_number' => $this->input->post('account_number'),
                       'bsb' => $this->input->post('bsb')
                     );
                }
                $this->customer->update_payment_details($customer_id, $payment_details);
            }
    }

}

?>
